package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.TitleBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Title;
import com.sp.scmdb.service.TitleService;

@Controller("titleAction")
@Scope("prototype")
public class TitleAction extends BaseAction<TitleBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(TitleAction.class);

	private TitleService titleService;

	@Autowired
	public void setTitleService(TitleService titleService) {
		this.titleService = titleService;
	}

	public String findAll() {

		try {
			List<Title> depList = titleService.queryAll();
			entity.setAllList(depList);
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity.getLastRow()));

			this.button("Super.Popup(5,'添加类别 ','/SCMDB/title/titleNew.jsp','',400,120)",
					"Super.editData('/SCMDB/titleAction!findTitle.action?id=',400,120)",
					"Super.delData('/SCMDB/titleAction!delTitle.action?id=')");
		} catch (RuntimeException e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}

	/**
	 * 添加职称
	 */
	public String addTitle() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = titleService.newTitle(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result, "类别");
		} catch (RuntimeException e) {
			log.error("addTitle fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("addTitle fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至职称修改页面
	 */
	public String findTitle() {
		try {
			String depId = request.getParameter("id");
			Title title = titleService.findID(depId);
			if (null != title) {
				entity.setId(String.valueOf(title.getId()));
				entity.setName(title.getName());
			} else {
				// 如果职称不存在，返回到查询页面
				return "commitSucc";
			}
		} catch (RuntimeException e) {
			log.error("findTitle fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findTitle fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}

	/**
	 * 更新职称
	 */
	public String editTitle() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = titleService.editTitle(entity, user);
			messageSession(result, "类别");
		} catch (RuntimeException e) {
			log.error("editTitle fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("editTitle fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 删除职称
	 */
	public String delTitle() {
		try {
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if (StringUtils.isNotEmpty(depId)) {
				String result = titleService.delTitle(depId.split(","), user);
				messageSession(result, "类别");
			}
		} catch (RuntimeException e) {
			log.error("delTitle fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("delTitle fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}	

}
